import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';

import Department from '../../db/models/department'

export let departmentType = new GraphQLObjectType({
    name: 'department',
    fields: {
        id: {
            type: GraphQLInt
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    }
})

export const departmentList = {
    type: new GraphQLList(departmentType),
    args: {},
    resolve(root, params, options) {
        return Department.findAll({
            order: ["department"]
        })
    }
}

export const departmentOne = {
    type: departmentType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Department.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const departmentUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Department.update({
            department: params.department,
            company: params.company
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const departmentAdd = {
    type: departmentType,
    args: {
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Department.create({
            department: params.department,
            company: params.company
        })
    }
}

export const departmentDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Department.destroy({
            where: {
                id: params.id
            }
        })
    }
}
